/**
 * 全局 And 引用常用组件
 */
import {
  Button, Icon, Space, Breadcrumb, Dropdown, Menu,
  // 布局类组件
  Layout, Row, Col,
  // 列表及Tab类组件
  Table, Pagination, Tabs,
  // 表单类组件
  Form, FormModel,
  AutoComplete, Cascader, Checkbox, DatePicker, Input, InputNumber, Mentions,
  Radio, Select, Rate, Slider, Switch, TimePicker, TreeSelect, Upload, Transfer,
  // 数据展示类
  Avatar, Badge, Descriptions, Empty, Popover, Tag, Tooltip, Tree, Collapse, List,
  // 反馈类
  Alert, Drawer, message, Modal, notification, Popconfirm, Spin,
  // 其他
  Divider
} from 'ant-design-vue'

import { Tree as VcTree } from 'ant-design-vue/es/vc-tree/src/Tree.js'
import { posToArr } from 'ant-design-vue/es/vc-tree/src/util.js'

/**
 * 全局引入自定义组件
 */
import ZSwitchOneZero from './switch-one-zero'

const install = (Vue) => {
  if (install.installed) {
    return
  }
  Vue.use(Button)
  Vue.use(Icon)
  Vue.use(Space)
  Vue.use(Breadcrumb)
  Vue.use(Dropdown)
  Vue.use(Menu)
  Vue.use(Layout)
  Vue.use(Row)
  Vue.use(Col)
  Vue.use(Table)
  Vue.use(Pagination)
  Vue.use(Tabs)
  Vue.use(Form)
  Vue.use(FormModel)
  Vue.use(AutoComplete)
  Vue.use(Cascader)
  Vue.use(Checkbox)
  Vue.use(DatePicker)
  Vue.use(Input)
  Vue.use(InputNumber)
  Vue.use(Mentions)
  Vue.use(Radio)
  Vue.use(Select)
  Vue.use(Rate)
  Vue.use(Slider)
  Vue.use(Switch)
  Vue.use(TimePicker)
  Vue.use(TreeSelect)
  Vue.use(Upload)
  Vue.use(Transfer)
  Vue.use(Avatar)
  Vue.use(Badge)
  Vue.use(Descriptions)
  Vue.use(Empty)
  Vue.use(Popover)
  Vue.use(Tag)
  Vue.use(Tooltip)
  Vue.use(Tree)
  Vue.use(Collapse)
  Vue.use(List)
  Vue.use(Alert)
  Vue.use(Drawer)
  Vue.use(message)
  Vue.use(Modal)
  Vue.use(Popconfirm)
  Vue.use(Spin)
  Vue.use(Divider)

  /**
   * 重写 onNodeDrop 取消节点拖拽正常操作中打印的警告信息
   * @param {*} event
   * @param {*} node
   * @returns
   */
  VcTree.methods.onNodeDrop = function (event, node) {
    var _$data2 = this.$data,
      _$data2$_dragNodesKey = _$data2._dragNodesKeys,
      _dragNodesKeys = _$data2$_dragNodesKey === undefined ? [] : _$data2$_dragNodesKey,
      _dropPosition = _$data2._dropPosition

    var eventKey = node.eventKey,
      pos = node.pos

    this.setState({
      _dragOverNodeKey: ''
    })

    if (_dragNodesKeys.indexOf(eventKey) !== -1) {
      return
    }

    var posArr = posToArr(pos)

    var dropResult = {
      event: event,
      node: node,
      dragNode: this.dragNode,
      dragNodesKeys: _dragNodesKeys.slice(),
      dropPosition: _dropPosition + Number(posArr[posArr.length - 1]),
      dropToGap: false
    }

    if (_dropPosition !== 0) {
      dropResult.dropToGap = true
    }
    this.__emit('drop', dropResult)
    this.dragNode = null
  }

  Vue.prototype.$components = Vue.options.components

  /**
   * 注册全局常用自定义组件
   */
  Vue.use(ZSwitchOneZero)

  // 模态弹窗提示组件
  Vue.prototype.confirm = Modal.confirm
  Vue.prototype.info = Modal.info
  Vue.prototype.success = Modal.success
  Vue.prototype.error = Modal.error
  Vue.prototype.warning = Modal.warning

  Vue.prototype.$confirm = Vue.prototype.confirm
  Vue.prototype.$info = Vue.prototype.info
  Vue.prototype.$success = Vue.prototype.success
  Vue.prototype.$error = Vue.prototype.error
  Vue.prototype.$warning = Vue.prototype.warning

  // toast 消息提示框组件
  Vue.prototype.message = message

  message.config({
    duration: 2
  })

  // 通知提示组件
  Vue.prototype.notify = Vue.prototype.$notify = {
    error (message, callback, duration = 2) {
      notification.error({
        message: '错误',
        description: message,
        duration: duration,
        onClose: callback,
        style: {
          whiteSpace: 'pre-wrap'
        }
      })
    },
    info (message, callback, duration = 2) {
      notification.info({
        message: '提示',
        description: message,
        duration: duration,
        onClose: callback,
        style: {
          whiteSpace: 'pre-wrap'
        }
      })
    },
    warn (message, callback, duration = 2) {
      notification.warn({
        message: '警告',
        description: message,
        duration: duration,
        onClose: callback,
        style: {
          whiteSpace: 'pre-wrap'
        }
      })
    },
    destroy () {
      notification.destroy()
    }
  }

  notification.config({
    duration: 2
  })
}

export default {
  install
}
